// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    // update (dt) {},

    doSendTdRegistered:function (userId, userName) {
        if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "tdOnRegistered","(Ljava/lang/String;Ljava/lang/String;)V",userId,userName);
        }else if (cc.sys.isNative && cc.sys.os === cc.sys.OS_IOS) {
            cc.warn("IOS系统")
        }else {
            if(!CC_DEBUG &&  typeof TDAPP !== "undefined"){
                var kv = { "UserId":userId}
                TDAPP.onEvent("user_registered",null,kv)
            }
        }
    },

    doSendTdLogin:function (userId, userName) {
        if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "tdOnLogin","(Ljava/lang/String;Ljava/lang/String;)V",userId,userName);
        }else if (cc.sys.isNative && cc.sys.os === cc.sys.OS_IOS) {
            cc.warn("IOS系统")
        }else {
            if(!CC_DEBUG && typeof TDAPP !== "undefined"){
                var kv = { "UserId":userId}
                TDAPP.onEvent("user_login",null,kv)
            }

        }
    },

    doSendTdPageStart:function (pageName) {
        if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "tdOnPageStart","(Ljava/lang/String;)V",pageName);
        }else if (cc.sys.isNative && cc.sys.os === cc.sys.OS_IOS) {
            cc.warn("IOS系统")
        }else {
            if(!CC_DEBUG &&  typeof TDAPP !== "undefined"){
                var kv = { "PageName":pageName}
                TDAPP.onEvent("page_start",null,kv)
            }

        }
    },

    doSendTdPageEnd:function (pageName) {
        if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "tdOnPageEnd","(Ljava/lang/String;)V",pageName);
        }else if (cc.sys.isNative && cc.sys.os === cc.sys.OS_IOS) {
            cc.warn("IOS系统")
        }else {
            if(!CC_DEBUG &&  typeof TDAPP !== "undefined"){
                var kv = { "PageEnd":pageName}
                TDAPP.onEvent("page_start",null,kv)
            }

        }
    },

    doSendTdEvent:function (eventName, eventLabel,mapString) {
        if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "tdOnEvent","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",eventName,eventLabel,mapString || null);
        }else if (cc.sys.isNative && cc.sys.os === cc.sys.OS_IOS) {
            cc.warn("IOS系统")
        }else {
            //example
            // let kv = {
            //     keys:["房间类型","房间名称","房间人数"],
            //     values:[cc.gg.enumConfig.getNameByRoomType(this.roomObj.type),this.roomObj.name,this.roomObj.p_max.toString()]
            // }
            // var kv = {“广告内容”: “电商双12大促“, “促销品价格”: ”100~400RMB“, “广告素材类型”: ”图片”}；
            if(!CC_DEBUG &&  typeof TDAPP !== "undefined"){
                let finalJson = {}
                if(mapString){
                    let json = JSON.parse(mapString)
                    let keys = json.keys
                    let values = json.values

                    let finalJson = {}
                    for(let i=0;i<keys.length;i++){
                        let k = keys[i]
                        finalJson[k] = values[i]
                    }
                }

                cc.log("td发送普通事件，附加数据是：",finalJson)
                TDAPP.onEvent(eventName,eventLabel,finalJson)
            }
        }
    },

    sendDebugEvent(){

    },

    sendTdClickOtpOnLoginPage:function () {
        this.doSendTdEvent("click_otp","page_login")
    },
});
